package com.example.city_home_app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_Post;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_Post;
import com.example.city_home_app.RetrofitListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CommunityActivity extends BaseActivity {
    @BindView(R.id.img_toolbar_community_add)
    ImageView imgToolbarCommunityAdd;
    @BindView(R.id.img_toolbar_community_focus)
    ImageView imgToolbarCommunityFocus;
    @BindView(R.id.toolbar_community_community)
    Toolbar toolbarCommunityCommunity;
    @BindView(R.id.tv_community_community_new)
    TextView tvCommunityCommunityNew;
    @BindView(R.id.tv_community_community_hot)
    TextView tvCommunityCommunityHot;
    @BindView(R.id.tv_community_community_lastback)
    TextView tvCommunityCommunityLastback;
    @BindView(R.id.rec_community_community_bottem)
    RecyclerView recCommunityCommunityBottem;
    @BindView(R.id.refresh_community_community_bottem)
    SwipeRefreshLayout refreshCommunityCommunityBottem;

    private int communityid;
    private ApiModel model = new ApiModel();
    private SwipeRefreshLayout refreshLayout;
    private Adapter_Post adapter_post;
    private int currentpage = 1,userid;
    private String communityname;
    private Boolean isfocus;

    private RetrofitListener<List<Down_Post>> retrofitListener = new RetrofitListener<List<Down_Post>>() {
        @Override
        public void onResponse(List<Down_Post> listener) {
            adapter_post.setList(listener);
            adapter_post.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> isfocuslistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                isfocus = true;
                imgToolbarCommunityFocus.setImageResource(R.drawable.ic_follow);
            }else{
                isfocus = false;
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    private RetrofitListener<Down_Universe> focuslistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                isfocus = true;
                imgToolbarCommunityFocus.setImageResource(R.drawable.ic_follow);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    private RetrofitListener<Down_Universe> unfocuslistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                isfocus = false;
                imgToolbarCommunityFocus.setImageResource(R.drawable.ic_follow_no);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_community;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        recCommunityCommunityBottem = (RecyclerView)findViewById(R.id.rec_community_community_bottem);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recCommunityCommunityBottem.setLayoutManager(layoutManager);
        adapter_post = new Adapter_Post(this);
        recCommunityCommunityBottem.setAdapter(adapter_post);
        recCommunityCommunityBottem.setItemAnimator(new DefaultItemAnimator());

        refreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh_community_community_bottem);
        refreshLayout.setColorSchemeColors(0xff49afcd);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        showToast();

    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        communityid = intent.getIntExtra("id",0);
        communityname = intent.getStringExtra("name");
        toolbarCommunityCommunity.setTitle(communityname);
        userid = BaseActivity.getUserinfo().getUserid();
        SelectBottem(1);
        model.isfocuscommunity(communityid,userid,isfocuslistener);
    }



    @OnClick({R.id.img_toolbar_community_add, R.id.img_toolbar_community_focus, R.id.tv_community_community_new, R.id.tv_community_community_hot, R.id.tv_community_community_lastback})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_toolbar_community_add:
                Intent intent = new Intent(this,PostAddActivity.class);
                intent.putExtra("id",communityid);
                intent.putExtra("type",0);
                startActivity(intent);
                break;
            case R.id.img_toolbar_community_focus:
                if(isfocus){
                    model.unfocuscommunity(communityid,userid,unfocuslistener);
                }else{
                    model.focuscommunity(communityid,userid,focuslistener);
                }
                break;
            case R.id.tv_community_community_new:
                SelectBottem(1);
                break;
            case R.id.tv_community_community_hot:
                SelectBottem(2);
                break;
            case R.id.tv_community_community_lastback:
                SelectBottem(3);
                break;
        }
    }

    private void showToast() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        toolbarCommunityCommunity = (Toolbar) findViewById(R.id.toolbar_community_community);
        setSupportActionBar(toolbarCommunityCommunity);
        toolbarCommunityCommunity.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        model.getpostlistforcommunity(currentpage,BaseActivity.getUserinfo().getCityid(),communityid,retrofitListener);
                        refreshLayout.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    private void SelectBottem(int type){
        tvCommunityCommunityNew.setTextColor(0x8A000000);
        tvCommunityCommunityHot.setTextColor(0x8A000000);
        tvCommunityCommunityLastback.setTextColor(0x8A000000);
        if(type == 1){
            currentpage = 1;
            tvCommunityCommunityNew.setTextColor(0xff49afcd);
        }
        if(type == 2){
            currentpage = 2;
            tvCommunityCommunityHot.setTextColor(0xff49afcd);
        }
        if(type == 3){
            currentpage = 3;
            tvCommunityCommunityLastback.setTextColor(0xff49afcd);
        }
        refreshLayout.setRefreshing(true);
        refresh();
    }

    @Override
    public void onResume() {
        super.onResume();
        model.getpostlistforcommunity(1,BaseActivity.getUserinfo().getCityid(),communityid,retrofitListener);
    }
}
